<!-- 文章管理 -->
<script setup>
import CategoryComponents from './components/CategoryComponents.vue'
import { ref } from 'vue'
import {
  articleGetListService,
  articleDeleServicle
} from '@/api/article.js'
import { fomatTime } from '@/utils/format.js'
import {
  Edit,
  Delete
} from '@element-plus/icons-vue'
import ArticleDrawer from '@/components/ArticleDrawer.vue'
// const selectVaue = ref(25215)
const params = ref({
  pagenum: 1,
  pagesize: 5,
  cate_id: '',
  state: ''
})
const loading = ref(false)
const articleList = ref()
const total = ref()
const getList = async () => {
  loading.value = true
  const res = await articleGetListService(
    params.value
  )
  articleList.value = res.data.data
  total.value = res.data.total
  loading.value = false
}
getList()
const onChange = async (page, size) => {
  if (params.value.pagesize === size) {
    params.value.pagenum = page
    await getList()
  } else {
    params.value.pagenum = 1
    params.value.pagesize = size
    await getList()
  }
}
const search = async () => {
  params.value.pagenum = 1
  await getList()
}
const reset = async () => {
  params.value.pagenum = 1
  params.value.cate_id = ''
  params.value.state = ''
  await getList()
}

//打开抽屉
const drawerRef = ref()
const onOpenDrawer = () => {
  drawerRef.value.open({})
}
//编辑
const articleEdit = (row) => {
  drawerRef.value.open(row)
}
//删除
const articleDelete = async (row) => {
  console.log(row.id)
  await articleDeleServicle(row.id)
  ElMessage.success('删除成功！')
  getList()
}

// 添加修改成功
const onSuccess = (type) => {
  if (type === 'add') {
    // 如果是添加，需要跳转渲染最后一页，编辑直接渲染当前页
    const lastPage = Math.ceil(
      (total.value + 1) / params.value.pagesize
    )
    params.value.pagenum = lastPage
  }
  getList()
}
</script>

<template>
  <pageComponent title="文章管理">
    <template #button>
      <el-button
        plain
        type="primary"
        round
        @click="onOpenDrawer"
        >发布文章</el-button
      >
    </template>

    <!-- 表单部分 -->
    <el-form inline>
      <el-form-item label="文章分类">
        <CategoryComponents
          v-model="params.cate_id"
        ></CategoryComponents>
      </el-form-item>
      <el-form-item label="发布状态">
        <el-select
          style="width: 200px"
          v-model="params.state"
        >
          <el-option
            label="已发布"
            value="已发布"
          ></el-option>
          <el-option
            label="草稿"
            value="草稿"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button @click="search" type="primary"
          >搜索</el-button
        >
        <el-button @click="reset">重置</el-button>
      </el-form-item>
    </el-form>

    <!-- 表格部分 -->
    <el-table
      :data="articleList"
      v-loading="loading"
    >
      <el-table-column
        label="文章标题"
        prop="title"
      ></el-table-column>
      <el-table-column
        label="分类"
        prop="cate_bane"
      ></el-table-column>
      <el-table-column
        label="发表时间"
        prop="pub_date"
      >
        <template #default="{ row }">
          {{ fomatTime(row.pub_date) }}
        </template>
      </el-table-column>
      <el-table-column
        label="状态"
        prop="state"
      ></el-table-column>
      <el-table-column label="操作" prop="">
        <template #default="row">
          <el-button
            type="primary"
            :icon="Edit"
            circle
            plain
            @click="articleEdit(row.row)"
          />
          <el-button
            type="danger"
            :icon="Delete"
            circle
            plain
            @click="articleDelete(row.row)"
          />
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页部分 -->
    <div class="demo-pagination-block">
      <el-pagination
        v-model:current-page="params.pagenum"
        v-model:page-size="params.pagesize"
        :page-sizes="[2, 5, 8, 11]"
        :background="true"
        layout="jumper, total, sizes, prev, pager, next"
        :total="total"
        @change="onChange"
      />
    </div>
    <!-- 抽屉部分 -->
    <ArticleDrawer
      ref="drawerRef"
      @success="onSuccess"
    ></ArticleDrawer>
  </pageComponent>
</template>

<style scoped></style>
